Skip to content

Conversation

@junhaoliao
Copy link
Member

@junhaoliao junhaoliao commented Oct 13, 2025

- Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379).
- Reduce layers using multi-line ENV (fixes #1378).

Description

This PR:

  • Moves the USER and ENV directives to after the image flattening step in the clp-package Dockerfile. This ensures that the environment variables and user settings are correctly applied to the final image, rather than being overridden or lost during the flattening process. (fixes Docker image flattening loses USER and ENV directives in clp-package Dockerfile #1379)
    • Creates the clp-user user with a home directory and shell in the final image, ensuring that the user environment is properly set up.
    • Sets the working directory to /opt/clp for the clp-user, ensuring that the user starts in the correct location when running a container from this image.
    • Ensures that the PATH includes both /opt/clp/bin and /opt/clp/sbin, allowing access to all installed binaries.
  • Reduces the number of layers in the Docker image by combining multiple ENV directives into a single multi-line ENV directive. This helps to optimize the image size and improve build performance. (fixes Optimize ENV declarations in clp-package Dockerfile to reduce layers #1378)

Checklist

  • The PR satisfies the contribution guidelines.
  • This is a breaking change and that has been indicated in the PR title, OR this isn't a
    breaking change.
  • Necessary docs have been updated, OR no docs need to be updated.

Validation performed

before

without the changes in the PR

junhao@ASUS-X870E:~/workspace/2-clp/tools/docker-images/clp-package$ ./build.sh
[+] Building 1.1s (12/12) FINISHED                                                                                                                                                                                  docker:default
...
junhao@ASUS-X870E:~/workspace/2-clp/tools/docker-images/clp-package$ docker run -it --rm "$(docker images --format '{{.Repository}}:{{.Tag}}' \
  | grep 'clp-package' | head -n1)" /bin/bash
root@563207a4425c:/# echo $SHELL
/bin/bash
root@563207a4425c:/# env
HOSTNAME=563207a4425c
PWD=/
HOME=/root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
TERM=xterm
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
root@563207a4425c:/# id
uid=0(root) gid=0(root) groups=0(root)

after

with the changes in the PR

junhao@ASUS-X870E:~/workspace/2-clp/tools/docker-images/clp-package$ ./build.sh 
[+] Building 5.3s (14/14) FINISHED                                                                                                                                                                                   docker:default
...
junhao@ASUS-X870E:~/workspace/2-clp/tools/docker-images/clp-package$ docker run -it --rm "$(docker images --format '{{.Repository}}:{{.Tag}}' \
  | grep 'clp-package' | head -n1)" /bin/bash
clp-user@6984500ebb62:~$ echo $SHELL
/bin/bash
clp-user@6984500ebb62:~$ env
HOSTNAME=6984500ebb62
PWD=/opt/clp
CLP_HOME=/opt/clp
HOME=/opt/clp
PYTHONPATH=/opt/clp/lib/python3/site-packages
TERM=xterm
USER=clp-user
SHLVL=1
PATH=/opt/clp/sbin:/opt/clp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
clp-user@6984500ebb62:~$ id
uid=1000(clp-user) gid=1000(clp-user) groups=1000(clp-user)

Observed that:

  1. The image size remains the same: 827MB -> 827MB. (Not shown from above output.)
  2. Number of layers changed: [+] Building 1.1s (12/12) -> Building 5.3s (14/14).
  3. UID / GID were set as expected: uid=0(root) gid=0(root) groups=0(root) -> uid=1000(clp-user) gid=1000(clp-user) groups=1000(clp-user).
  4. The PATH was set as expected: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -> PATH=/opt/clp/sbin:/opt/clp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin.
  5. Env var LS_COLORS was lost, which is even better.
  6. CLP_HOME is set as expected: CLP_HOME missing -> CLP_HOME=/opt/clp.
  7. HOME is set as expected: HOME=/root -> HOME=/opt/clp.
  8. PYTHONPATH is set as expected: PYTHONPATH missing -> PYTHONPATH=/opt/clp/lib/python3/site-packages.

Summary by CodeRabbit

  • Chores
    • Moved runtime environment and user setup into the final image stage for a cleaner runtime.
    • Flattened the final image to reduce intermediate layers and produce a leaner runtime artifact.
    • Consolidated environment variables (application home, PATH including sbin/bin, and PYTHONPATH) for simpler configuration.
    • Added a dedicated runtime user with configurable UID, ensured application files are owned accordingly, and set the working directory to the application home.

…lp-package Dockerfile (fixes y-scope#1379); Reduce layers using multi-line ENV (fixes y-scope#1378).
@junhaoliao junhaoliao requested a review from a team as a code owner October 13, 2025 02:44
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 13, 2025

Walkthrough

Moved runtime ENV and USER metadata and package ownership into a final scratch stage in tools/docker-images/clp-package/Dockerfile. The final stage now copies the base filesystem, creates clp-user with configurable UID, sets CLP_HOME, consolidated PATH, PYTHONPATH, USER, WORKDIR, and performs a chown copy of the package into CLP_HOME.

Changes

Cohort / File(s) Summary of changes
Final-stage Dockerfile additions
tools/docker-images/clp-package/Dockerfile
Added a final FROM scratch stage that COPY --from=base / /, defines ARG UID, sets consolidated multi-line ENV (CLP_HOME, PATH including sbin, PYTHONPATH), creates clp-user with the provided UID, sets USER="clp-user", and WORKDIR to CLP_HOME.
Ownership and copy adjustments
tools/docker-images/clp-package/Dockerfile
Removed runtime USER/ENV declarations from the base stage and the prior base-to-/opt/clp copy; changed final package copy to use --chown=${UID} into CLP_HOME to ensure correct ownership in the flattened image.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    actor Builder as Docker Builder
    participant Base as "base stage (build)"
    participant Final as "final stage (scratch)"
    participant Image as "resulting image"

    Builder->>Base: execute build steps → produce filesystem/artifacts
    Note over Base: No runtime ENV/USER left here
    Builder->>Final: FROM scratch\nCOPY --from=base / /
    Final->>Final: ARG UID\ncreate clp-user (useradd) with UID\nset multi-line ENV (CLP_HOME, PATH, PYTHONPATH)\nset USER and WORKDIR\nCOPY --chown=${UID} clp-package → ${CLP_HOME}
    Final->>Image: persist ENV and USER metadata in final image
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title clearly and specifically summarizes the main changes in the pull request: creating a non-root clp-user, moving USER and ENV directives to the final stage after image flattening, and consolidating ENV statements to reduce layers. All three key changes mentioned in the title are directly present in the changeset, with specific issue references provided. The title is concise, descriptive, and sufficient for a teammate scanning the history to understand the primary purpose of this modification.
Linked Issues Check ✅ Passed The changes directly address both linked issues. For issue #1379, the PR removes USER and ENV directives from the base stage and re-establishes them in the final scratch stage after the COPY --from=base / / command, ensuring that CLP_HOME, PATH modifications, PYTHONPATH, and USER configurations are preserved in the final image. For issue #1378, the changes consolidate multiple ENV statements into a single multi-line ENV statement to reduce Docker layers. The PR objectives and validation results confirm both requirements have been implemented successfully, with the container now running as a non-root user with proper environment variables set.
Out of Scope Changes Check ✅ Passed All changes in the pull request are directly scoped to the requirements defined in the linked issues. The modifications to tools/docker-images/clp-package/Dockerfile focus exclusively on moving USER and ENV directives to the final stage, consolidating ENV statements to reduce layers, and creating a non-root user with proper configuration. There are no unrelated modifications or tangential changes beyond what is necessary to address issues #1379 and #1378.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dba99b7 and 2967a64.

📒 Files selected for processing (1)
  • tools/docker-images/clp-package/Dockerfile (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).
Learnt from: junhaoliao
PR: y-scope/clp#1413
File: tools/docker-images/clp-package/Dockerfile:22-24
Timestamp: 2025-10-20T21:05:30.417Z
Learning: In the clp repository's Dockerfiles, ENV directives should be consolidated into multi-line ENV statements when possible to reduce image layers. ENV statements should only be split into separate commands when consolidation is not possible due to dependencies (e.g., when later variables must reference earlier ones that need to be set first, or when PATH must be modified sequentially).
📚 Learning: 2025-10-13T03:32:19.293Z
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).

Applied to files:

  • tools/docker-images/clp-package/Dockerfile
📚 Learning: 2025-10-20T21:05:30.417Z
Learnt from: junhaoliao
PR: y-scope/clp#1413
File: tools/docker-images/clp-package/Dockerfile:22-24
Timestamp: 2025-10-20T21:05:30.417Z
Learning: In the clp repository's Dockerfiles, ENV directives should be consolidated into multi-line ENV statements when possible to reduce image layers. ENV statements should only be split into separate commands when consolidation is not possible due to dependencies (e.g., when later variables must reference earlier ones that need to be set first, or when PATH must be modified sequentially).

Applied to files:

  • tools/docker-images/clp-package/Dockerfile
🪛 Checkov (3.2.334)
tools/docker-images/clp-package/Dockerfile

[low] 1-29: Ensure that HEALTHCHECK instructions have been added to container images

(CKV_DOCKER_2)

🔇 Additional comments (2)
tools/docker-images/clp-package/Dockerfile (2)

18-29: Implementation correctly addresses PR objectives and follows consolidation policy.

The changes properly move USER and ENV directives into the final scratch stage, ensuring they persist in the resulting image. The ENV consolidation (lines 21–23) correctly groups related variables (PATH sbin/bin modification, PYTHONPATH, and USER) into a single multi-line statement per the latest guidance from PR #1413, while keeping CLP_HOME separate since it's referenced by subsequent ENV variables.

The use of ${UID} in the COPY --chown (line 29) instead of ${USER} correctly works around the Docker Buildx issue noted in the prior review. The useradd command (line 25) will create the clp-user with proper UID and home-directory ownership, and the WORKDIR is set to the home directory as intended.


1-29: Low-severity static analysis flag: Missing HEALTHCHECK.

Checkov flags the absence of a HEALTHCHECK instruction. This is a common false positive for applications that do not expose health-check endpoints. If this container is not expected to implement health-check probes, this flag can safely be ignored. If health-check functionality is desired later, a HEALTHCHECK directive can be added.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix #1410, we need to "Move USER and ENV directives after image flattening in clp-package Dockerfile"

Comment on lines 19 to 22
ENV CLP_HOME="/opt/clp"
ENV PATH="${CLP_HOME}/bin:${PATH}" \
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages"
ENV PATH="${CLP_HOME}/sbin:${PATH}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We still have to keep three ENV directives because the later ENV settings depend on the former ones.
  2. Technically, we could combine the PATH settings into a single line like PATH="${CLP_HOME}/sbin:${CLP_HOME}/bin:${PATH}". For readability, we previously agreed in feat(docker): Add container image containing the CLP package, for Docker Compose integration (resolves #1164). #1166 that we want to split them into separate lines. Although the aim is to cut down on number of layers, we still should keep the lines separated given the benefits of readability should outweigh the layer count reduction

PYTHONPATH="${CLP_HOME}/lib/python3/site-packages"
ENV PATH="${CLP_HOME}/sbin:${PATH}"

USER 1000:1000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The messages of groups: cannot find name for group ID 1000 and I have no name! are a little scary. Could we find a way to make them disappear?

@junhaoliao junhaoliao marked this pull request as draft October 14, 2025 05:23
@junhaoliao junhaoliao changed the title fix(docker): Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379); Reduce layers using multi-line ENV (fixes #1378). fix(docker): Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379); Add non-root clp-user with proper home and PATH; Reduce layers using multi-line ENV (fixes #1378). Oct 14, 2025
@junhaoliao junhaoliao changed the title fix(docker): Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379); Add non-root clp-user with proper home and PATH; Reduce layers using multi-line ENV (fixes #1378). fix(docker): Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379); Add non-root clp-user with proper HOME and PATH; Reduce layers using multi-line ENV (fixes #1378). Oct 14, 2025
@junhaoliao junhaoliao changed the title fix(docker): Move USER and ENV directives after image flattening in clp-package Dockerfile (fixes #1379); Add non-root clp-user with proper HOME and PATH; Reduce layers using multi-line ENV (fixes #1378). fix(docker): Create non-root clp-user and move env setup after flattening (fixes #1378, fixes #1379). Oct 14, 2025
@junhaoliao junhaoliao changed the title fix(docker): Create non-root clp-user and move env setup after flattening (fixes #1378, fixes #1379). feat(docker): Create non-root clp-user and move env setup after flattening (fixes #1378, fixes #1379). Oct 14, 2025
@junhaoliao junhaoliao marked this pull request as ready for review October 14, 2025 06:48
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
tools/docker-images/clp-package/Dockerfile (1)

18-21: Restore one-variable-per-ENV instruction

We previously agreed (PR #1166) to keep each ENV directive focused on a single variable for readability and to avoid order-dependent surprises. Please split the combined ENV back into separate statements for PATH, PYTHONPATH, and USER, even if it costs an extra layer. Example:

-ENV PATH="${CLP_HOME}/bin:${PATH}"
-ENV PATH="${CLP_HOME}/sbin:${PATH}" \
-    PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \
-    USER="clp-user"
+ENV PATH="${CLP_HOME}/bin:${PATH}"
+ENV PATH="${CLP_HOME}/sbin:${PATH}"
+ENV PYTHONPATH="${CLP_HOME}/lib/python3/site-packages"
+ENV USER="clp-user"

Based on learnings.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7d18129 and 3ef8714.

📒 Files selected for processing (1)
  • tools/docker-images/clp-package/Dockerfile (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).
📚 Learning: 2025-10-13T03:32:19.293Z
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).

Applied to files:

  • tools/docker-images/clp-package/Dockerfile
🪛 Checkov (3.2.334)
tools/docker-images/clp-package/Dockerfile

[low] 1-25: Ensure that HEALTHCHECK instructions have been added to container images

(CKV_DOCKER_2)

@junhaoliao junhaoliao requested review from kirkrodrigues and sitaowang1998 and removed request for kirkrodrigues and sitaowang1998 October 20, 2025 20:46
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
tools/docker-images/clp-package/Dockerfile (1)

25-28: Confirm critical ownership issue and provide verified fix.

The review comment accurately identifies a blocking issue. Verification confirms:

  • Line 25 lacks --chown flag: files copied to ${CLP_HOME} remain owned by root (uid=0), blocking write access when the container runs as clp-user (uid=1000).
  • Line 27 lacks --user-group flag: useradd creates only a user without a corresponding group, causing group ID resolution warnings.

The proposed diff is correct and follows Docker best practices. Apply these changes:

-COPY --link ./build/clp-package ${CLP_HOME}
+COPY --link --chown=1000:1000 ./build/clp-package ${CLP_HOME}

-RUN useradd --uid 1000 --shell /bin/bash --home-dir ${CLP_HOME} ${USER}
+RUN useradd --uid 1000 --user-group --shell /bin/bash --home-dir ${CLP_HOME} ${USER}

Note: An alternative (more explicit) approach would be to create the group first using groupadd -g 1000 clp-user and then reference it with useradd -u 1000 -g clp-user, but the proposed fix is a valid functional improvement.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3ef8714 and 65f2d1b.

📒 Files selected for processing (1)
  • tools/docker-images/clp-package/Dockerfile (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).
📚 Learning: 2025-10-13T03:32:19.293Z
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).

Applied to files:

  • tools/docker-images/clp-package/Dockerfile
🪛 Checkov (3.2.334)
tools/docker-images/clp-package/Dockerfile

[low] 1-29: Ensure that HEALTHCHECK instructions have been added to container images

(CKV_DOCKER_2)

Comment on lines 21 to 23
ENV PATH="${CLP_HOME}/sbin:${PATH}" \
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \
USER="clp-user"
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

ENV consolidation contradicts prior agreement to keep lines split.

Lines 21-23 consolidate PATH and related settings into a multi-line ENV statement. However, based on learnings from PR #1414 and the prior agreement in PR #1166, ENV directives should remain split into separate lines for readability, especially since later ENV settings depend on earlier ones (e.g., PYTHONPATH and USER reference CLP_HOME). Please revert to separate ENV lines.

Apply this diff to split the ENV statements:

 ENV CLP_HOME="/opt/clp"
 ENV PATH="${CLP_HOME}/bin:${PATH}"
-ENV PATH="${CLP_HOME}/sbin:${PATH}" \
-    PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \
-    USER="clp-user"
+ENV PATH="${CLP_HOME}/sbin:${PATH}"
+ENV PYTHONPATH="${CLP_HOME}/lib/python3/site-packages"
+ENV USER="clp-user"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ENV PATH="${CLP_HOME}/sbin:${PATH}" \
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \
USER="clp-user"
ENV PATH="${CLP_HOME}/sbin:${PATH}"
ENV PYTHONPATH="${CLP_HOME}/lib/python3/site-packages"
ENV USER="clp-user"
🤖 Prompt for AI Agents
In tools/docker-images/clp-package/Dockerfile around lines 21 to 23, the ENV
entries were combined into a single multi-line ENV which contradicts the agreed
style and can break dependent expansions; revert to separate ENV directives so
each variable is set on its own line (e.g., one ENV for PATH, one for
PYTHONPATH, one for USER) ensuring later variables can safely reference CLP_HOME
and improving readability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we now only split if consolidation into the same command is not possible

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junhaoliao, thank you for the clarification! I understand now that the policy has been updated: ENV directives should be consolidated into multi-line statements when possible, and only split when dependencies between variables require separate commands.

The current implementation is correct under this updated guidance:

  • Line 19: ENV CLP_HOME="/opt/clp" — separate because it's referenced by subsequent ENVs
  • Line 20: ENV PATH="${CLP_HOME}/bin:${PATH}" — separate because PATH must be modified in sequence
  • Lines 21-23: Consolidated multi-line ENV for the second PATH modification, PYTHONPATH, and USER — correct since these can all be set together

I apologize for the confusion caused by my earlier comment based on the outdated policy from PR #1166. The consolidation here properly addresses issue #1378 (reducing layers) while maintaining correct dependency order.


✏️ Learnings added
Learnt from: junhaoliao
PR: y-scope/clp#1413
File: tools/docker-images/clp-package/Dockerfile:22-24
Timestamp: 2025-10-20T21:05:30.383Z
Learning: In the clp repository's Dockerfiles, ENV directives should be consolidated into multi-line ENV statements when possible to reduce image layers. ENV statements should only be split into separate commands when consolidation is not possible due to dependencies (e.g., when later variables must reference earlier ones that need to be set first, or when PATH must be modified sequentially).

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).

# Flatten the image
FROM scratch
COPY --link --from=base / /
COPY --link --chown=${UID} ./build/clp-package ${CLP_HOME}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although Docker's documentation states that --chown= supports usernames and performs lookups in /etc/passwd and /etc/group within the container to resolve UID/GID, we must use ${UID} instead of ${USER} here because such lookups are not yet supported when --link is used.

This limitation is a known issue in Docker Buildx, tracked at moby/buildkit#2987

junhaoliao added a commit to junhaoliao/clp that referenced this pull request Oct 20, 2025
@junhaoliao junhaoliao changed the title feat(docker): Create non-root clp-user and move env setup after flattening (fixes #1378, fixes #1379). feat(docker): Create non-root clp-user; Move USER and ENV directives after flattening (fixes #1379); Reduce layers using multi-line ENV (fixes #1378). Oct 21, 2025

ARG UID=1000
ENV CLP_HOME="/opt/clp"
ENV PATH="${CLP_HOME}/bin:${PATH}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PATH can be moved inside the multi line env with something like

PATH="${CLP_HOME}/sbin:${CLP_HOME}/bin:${PATH}"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For readability, would it be better to keep them on separate lines?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine from readability side. Appending two elements to a list is not hard to understand.

@junhaoliao junhaoliao merged commit 448068a into y-scope:main Oct 24, 2025
19 checks passed
PYTHONPATH="${CLP_HOME}/lib/python3/site-packages" \
USER="clp-user"

RUN useradd --uid ${UID} --shell /bin/bash --home-dir ${CLP_HOME} ${USER}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the user home path setting here broke the s3 profile auth type because the CONTAINER_AWS_CONFIG_DIRECTORY's value should be relative to the CLP_HOME but isn't

@coderabbitai create an issue to track this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Create a GitHub issue to track the S3 profile auth type breakage

gh issue create \
  --repo y-scope/clp \
  --title "S3 profile auth type broken due to user home path change in Docker image" \
  --body "## Problem

The user home path setting introduced in PR #1413 broke the S3 profile auth type because \`CONTAINER_AWS_CONFIG_DIRECTORY\`'s value should be relative to \`CLP_HOME\` but isn't.

## Context

- PR #1413 sets the home directory for \`clp-user\` to \`CLP_HOME\` (\`/opt/clp\`) in the Docker image
- \`CONTAINER_AWS_CONFIG_DIRECTORY\` is currently hardcoded to \`/.aws\`
- This mismatch breaks S3 profile authentication

## Related

- PR: https://github.com/y-scope/clp/pull/1413
- Comment: https://github.com/y-scope/clp/pull/1413#discussion_r2476592326
- Reported by: @junhaoliao" \
  --assignee junhaoliao

Length of output: 42


@junhaoliao, I've created issue #1524 to track the S3 profile auth type breakage caused by the user home path change. The issue documents how the CONTAINER_AWS_CONFIG_DIRECTORY hardcoded path conflicts with the new home directory setting.

#1524


🧠 Learnings used
Learnt from: junhaoliao
PR: y-scope/clp#1414
File: tools/docker-images/clp-package/Dockerfile:20-24
Timestamp: 2025-10-13T03:32:19.293Z
Learning: In the clp repository's Dockerfiles (e.g., tools/docker-images/clp-package/Dockerfile), ENV directives should be split into separate lines for readability rather than consolidated to reduce layer count. This is especially true for PATH modifications, as agreed upon in PR #1166. Later ENV settings may depend on earlier ones (e.g., referencing CLP_HOME).

Learnt from: junhaoliao
PR: y-scope/clp#1413
File: tools/docker-images/clp-package/Dockerfile:22-24
Timestamp: 2025-10-20T21:05:30.417Z
Learning: In the clp repository's Dockerfiles, ENV directives should be consolidated into multi-line ENV statements when possible to reduce image layers. ENV statements should only be split into separate commands when consolidation is not possible due to dependencies (e.g., when later variables must reference earlier ones that need to be set first, or when PATH must be modified sequentially).

Learnt from: junhaoliao
PR: y-scope/clp#1178
File: components/clp-package-utils/clp_package_utils/controller.py:217-223
Timestamp: 2025-09-25T05:13:13.298Z
Learning: The compression scheduler service in CLP runs with CLP_UID_GID (current user's UID:GID) rather than CLP_SERVICE_CONTAINER_UID_GID (999:999), unlike infrastructure services such as database, queue, redis, and results cache which run with the service container UID:GID.

Learnt from: junhaoliao
PR: y-scope/clp#1335
File: taskfiles/docker-images.yaml:15-15
Timestamp: 2025-09-25T19:26:32.436Z
Learning: In the CLP project's Taskfile, the `:package` task creates the G_PACKAGE_BUILD_DIR directory structure, so any task that depends on `:package` (like `docker-images:package`) can safely assume this directory exists without needing additional mkdir commands.

Learnt from: jackluo923
PR: y-scope/clp#1054
File: components/core/tools/docker-images/clp-env-base-musllinux_1_2-aarch64/build.sh:3-5
Timestamp: 2025-07-07T17:43:04.349Z
Learning: In CLP project build scripts (specifically build.sh files in docker-images directories), maintain consistency with the established pattern of using separate `set -eu` and `set -o pipefail` commands rather than combining them into `set -euo pipefail`, to ensure uniform script structure across all platform build scripts.

Learnt from: jackluo923
PR: y-scope/clp#1054
File: components/core/tools/docker-images/clp-env-base-musllinux_1_2-x86/build.sh:18-24
Timestamp: 2025-07-01T14:52:02.418Z
Learning: In the CLP project, consistency across platform build scripts is prioritized over defensive programming when it comes to git remote handling. All build.sh files in docker-images directories should follow the same pattern for git metadata injection.

Learnt from: jackluo923
PR: y-scope/clp#1054
File: components/core/tools/docker-images/clp-env-base-musllinux_1_2-x86/build.sh:18-24
Timestamp: 2025-07-01T14:52:02.418Z
Learning: In the CLP project, consistency across platform build scripts is prioritized over defensive programming when it comes to git remote handling. All build.sh files in docker-images directories should follow the same pattern for git metadata injection.

Learnt from: junhaoliao
PR: y-scope/clp#1178
File: components/clp-py-utils/clp_py_utils/clp_config.py:47-47
Timestamp: 2025-10-07T07:54:32.427Z
Learning: In components/clp-py-utils/clp_py_utils/clp_config.py, the CONTAINER_AWS_CONFIG_DIRECTORY constant is intentionally set to pathlib.Path("/") / ".aws" (i.e., `/.aws`) rather than a user-specific home directory. This hardcoded path is part of the container orchestration design.

Learnt from: Bill-hbrhbr
PR: y-scope/clp#1122
File: components/core/src/clp/clp/CMakeLists.txt:175-195
Timestamp: 2025-07-23T09:54:45.185Z
Learning: In the CLP project, when reviewing CMakeLists.txt changes that introduce new compression library dependencies (BZip2, LibLZMA, LZ4, ZLIB), the team prefers to address conditional linking improvements in separate PRs rather than expanding the scope of focused migration PRs like the LibArchive task-based installation migration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Docker image flattening loses USER and ENV directives in clp-package Dockerfile Optimize ENV declarations in clp-package Dockerfile to reduce layers

2 participants